package com.eastfair.messagecenter.duty;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.eastfair.log.point.PointUtil;
import com.eastfair.messagecenter.domain.TaskInfo;
import com.eastfair.messagecenter.enumeration.BusinessCode;
import com.eastfair.messagecenter.pipeline.BusinessProcess;
import com.eastfair.messagecenter.pipeline.ProcessContext;
import com.eastfair.messagecenter.pipeline.SendTaskModel;
import com.eastfair.messagecenter.point.SendPoint;
import com.eastfair.messagecenter.service.ConsumeService;
import com.eastfair.point.business.gateway.BusinBalckPoint;
import com.google.common.base.Throwables;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ligang
 * 将消息发送到MQ
 */
@Slf4j
@Service
public class SendMqAction implements BusinessProcess<SendTaskModel> {

    @Autowired
    PointUtil pointUtil;

    @Autowired
    ConsumeService consumeService;
    @Override
    public void process(ProcessContext<SendTaskModel> context) {
        SendTaskModel sendTaskModel = context.getProcessModel();
        try {
            if (BusinessCode.COMMON_SEND.equals(context.getCode())) {
                List<TaskInfo> taskInfoList = sendTaskModel.getTaskInfoList();
                //队列发送
                SendPoint sendPoint = new SendPoint();
                sendPoint.setTaskInfoLists(taskInfoList);
                sendPoint.setChannelType(taskInfoList.get(0).getSendChannel());
//                pointUtil.addBusinPointMsg(sendPoint);
                consumeService.consumeSend(sendPoint.getTaskInfoLists());
            }else if (BusinessCode.TIMED_SEND.equals(context.getCode())) {
                List<TaskInfo> taskInfoList = sendTaskModel.getTaskInfoList();
                //队列发送
                SendPoint sendPoint = new SendPoint();
                sendPoint.setTaskInfoLists(taskInfoList);
                sendPoint.setChannelType(taskInfoList.get(0).getSendChannel());
//                pointUtil.addBusinPointMsg(sendPoint);
                consumeService.consumeSend(sendPoint.getTaskInfoLists());
            }
        } catch (Exception e) {
            e.printStackTrace();
//            log.error("send {} fail! e:{},params:{}", mqPipeline, Throwables.getStackTraceAsString(e)
//                    , JSON.toJSONString(CollUtil.getFirst(sendTaskModel.getTaskInfo().listIterator())));
        }
    }

}
